# -*- coding: utf-8 -*-
# frontend/components/model_selector.py
"""
模型选择器组件
"""

import streamlit as st
from config.settings import SUPPORTED_MODELS
from typing import List

def render_model_selector() -> List[str]:
    """渲染模型选择器"""
    st.markdown("### 🤖 选择AI模型")
    
    # 显示模型信息
    cols = st.columns(len(SUPPORTED_MODELS))
    
    selected_models = []
    
    for i, (model_key, model_info) in enumerate(SUPPORTED_MODELS.items()):
        with cols[i]:
            # 检查API密钥是否配置
            api_keys = st.session_state.get("api_keys", {})
            provider_key = model_info["provider"].lower()
            
            is_available = api_keys.get(provider_key, "") != ""
            
            # 模型卡片
            card_style = "background-color: #f0f2f6;" if not is_available else f"border-left: 4px solid {model_info['color']}; background-color: #f9f9f9;"
            
            st.markdown(f"""
            <div style="padding: 1rem; border-radius: 8px; margin-bottom: 1rem; {card_style}">
                <h4 style="margin: 0; color: {model_info['color']};">
                    {model_info['avatar']} {model_key}
                </h4>
                <p style="margin: 0.5rem 0; font-size: 0.9rem; color: #666;">
                    {model_info['description']}
                </p>
                <small style="color: #999;">
                    提供商: {model_info['provider']}<br>
                    最大tokens: {model_info['max_tokens']}
                </small>
            </div>
            """, unsafe_allow_html=True)
            
            # 选择框
            if is_available:
                is_selected = st.checkbox(
                    f"选择 {model_key}",
                    value=model_key in st.session_state.get("selected_models", []),
                    key=f"select_{model_key}"
                )
                
                if is_selected:
                    selected_models.append(model_key)
            else:
                st.warning(f"需要配置 {model_info['provider']} API密钥")
    
    # 更新会话状态
    st.session_state.selected_models = selected_models
    
    # 显示选择摘要
    if selected_models:
        st.info(f"已选择 {len(selected_models)} 个模型: {', '.join(selected_models)}")
    else:
        st.warning("请至少选择一个AI模型")
    
    return selected_models